package com.hanthink.web.controller.app.report;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.hanthink.common.core.text.Convert;
import com.hanthink.system.domain.manhour.PLineEquipment;
import com.hanthink.system.domain.report.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.hanthink.common.annotation.Log;
import com.hanthink.common.constant.HttpStatus;
import com.hanthink.common.core.controller.BaseController;
import com.hanthink.common.core.domain.AjaxResult;
import com.hanthink.common.core.domain.model.LoginUser;
import com.hanthink.common.core.page.TableDataInfo;
import com.hanthink.common.enums.BusinessType;
import com.hanthink.common.utils.ServletUtils;
import com.hanthink.framework.web.service.TokenService;
import com.hanthink.system.mapper.report.IndexMapper;
import com.hanthink.system.service.repair.IRepairRecordInfoService;
import com.hanthink.system.service.repair.IRequestFileInfoService;
import com.hanthink.system.service.report.IReportInfoService;

/**
 * @author Administrator 报表信息查询
 *
 */
@RestController
@RequestMapping("/app/report")
public class ReportControll extends BaseController {

	@Autowired
	private IReportInfoService reportInfoService;

	@Autowired
	private IndexMapper indexMapper;

	@Autowired
	private TokenService tokenService;

	@Autowired
	private IRequestFileInfoService requestFileInfoService;

	@Autowired
	private IRepairRecordInfoService repairRecordInfoService;

	// 查询点检柱形图任务信息列表
	@PostMapping("/list")
	public TableDataInfo list(@RequestBody ReportInfo info) {
		List<ReportInfo> list = reportInfoService.queryReportInfo(info);
		TableDataInfo tableDataInfo = new TableDataInfo();
		tableDataInfo.setCode(HttpStatus.SUCCESS);
		tableDataInfo.setMsg("查询成功");
		tableDataInfo.setRows(list);
		return tableDataInfo;
	}

	// 查询点检表格任务信息列表
	@PostMapping("/tableList")
	public TableDataInfo tableList(@RequestBody ReportInfo info) {
		List<ReportInfo> list = reportInfoService.querytableReportInfo(info);
		TableDataInfo tableDataInfo = new TableDataInfo();
		tableDataInfo.setCode(HttpStatus.SUCCESS);
		tableDataInfo.setMsg("查询成功");
		tableDataInfo.setRows(list);
		return tableDataInfo;
	}

	@PostMapping("/queryList")
	@Log(title = "已完成工艺点检查询", businessType = BusinessType.OTHER)
	public TableDataInfo queryList(@RequestBody ReportInfo info) {
		List<ReportInfo> list = new ArrayList<>();
		if (info.getType() == 1 && info.getTaskType().equals("3")) {
			 list = reportInfoService.queryListPlus(info);
		}
		else
		{
			 list = reportInfoService.queryList(info);
		}

		TableDataInfo tableDataInfo = new TableDataInfo();
		tableDataInfo.setCode(HttpStatus.SUCCESS);
		tableDataInfo.setMsg("查询成功");
		tableDataInfo.setRows(list);
		return tableDataInfo;
	}

	// 查询工厂信息列表
	@PostMapping("/siteList")
	public TableDataInfo siteList() {
		SiteInfo info =  new SiteInfo();
		LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
		if (loginUser.getUser().getDept().getParentId() != 0L && loginUser.getUser().getSite() != null) {
			info.setSiteKey(loginUser.getUser().getSite());
		}
		if (loginUser.getUser().getDept().getParentId() != 0L && loginUser.getUser().getHangame() != null) {
			info.setHangameKey(loginUser.getUser().getHangame());
		}
		List<SiteInfo> list = reportInfoService.querySiteInfo(info);
		TableDataInfo tableDataInfo = new TableDataInfo();
		tableDataInfo.setCode(HttpStatus.SUCCESS);
		tableDataInfo.setMsg("查询成功");
		tableDataInfo.setRows(list);
		return tableDataInfo;
	}

	// 查询部门信息列表
	@PostMapping("/departList")
	public TableDataInfo departList(@RequestBody ReportInfo info) {
		List<ReportInfo> list = reportInfoService.queryDepartInfo(info);
		TableDataInfo tableDataInfo = new TableDataInfo();
		tableDataInfo.setCode(HttpStatus.SUCCESS);
		tableDataInfo.setMsg("查询成功");
		tableDataInfo.setRows(list);
		return tableDataInfo;
	}

	// 查询未完成功能维修信息
	@PostMapping("/repairList")
	public TableDataInfo repairList(@RequestBody ReportInfo info) {
		List<ReportInfo> list = reportInfoService.queryRepairReportInfo(info);
		TableDataInfo tableDataInfo = new TableDataInfo();
		tableDataInfo.setCode(HttpStatus.SUCCESS);
		tableDataInfo.setMsg("查询成功");
		tableDataInfo.setRows(list);
		return tableDataInfo;
	}

	@PostMapping("/querynoRequestAndRepair")
	public TableDataInfo querynoRequestAndRepair(@RequestBody RequestAndRepairRecord requestAndRepairRecord)  throws ParseException {
		List<RequestAndRepairRecordByTime> list = reportInfoService.queryRequestAndRepairbyTime(requestAndRepairRecord);
		List<RequestAndRepairRecordByTime> listnew = null;
		for (RequestAndRepairRecordByTime requestAndRepairRecordByTime : list) {
			RepairTimeRecord rt = new RepairTimeRecord();
			rt.setRepairId(Convert.toLong(requestAndRepairRecordByTime.getRepairId()));
			List<RepairTimeRecord> lsdt= reportInfoService.queryRepairTimeRecord(rt);
			for (RepairTimeRecord repairTimeRecord : lsdt) {
				requestAndRepairRecordByTime.setStartTime(repairTimeRecord.getStartTime());
				requestAndRepairRecordByTime.setEndTime(repairTimeRecord.getEndTime());
			}
		}
		TableDataInfo tableDataInfo = new TableDataInfo();
		tableDataInfo.setCode(HttpStatus.SUCCESS);
		tableDataInfo.setMsg("操作成功");
		tableDataInfo.setRows(list);
		return tableDataInfo;
	}

	// 查询具体信息


	// 查询我已完成的任务
	@PostMapping("/queryMyTaskList")
	public TableDataInfo queryMyTaskList(@RequestBody ReportInfo info) {
		startPage();
		// 获取当前登录用户
		LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
		info.setExecuter(loginUser.getUser().getUserName());
		List<ReportInfo> list = reportInfoService.queryMyTaskList(info);
		return getDataTable(list);
	}

	// 查询首页看板数据
	@PostMapping("/indexData")
	public AjaxResult indexData(@RequestBody IndexInfo indexInfo) {
		AjaxResult ajax = AjaxResult.success();
		// 查询设备运行状态
		ajax.put("equipmentRunStatus", indexMapper.queryEquipmentStatus(indexInfo));
		// 查询今日报修
		ajax.put("repairOfToday", indexMapper.queryRepairOfToday(indexInfo));
		// 当月故障排行榜
		ajax.put("faultRankMonth", indexMapper.queryFaultRankMonth(indexInfo));
		// 点检任务统计
		ajax.put("pointTask", indexMapper.queryPointTask(indexInfo));
		// 待执行点检任务
		ajax.put("waitExecuteTask", indexMapper.queryWaitExecuteTask(indexInfo));
		// 自修统计
		ajax.put("selfRepair", indexMapper.querySelfRepair(indexInfo));
		return ajax;
	}

	// 查询报修和维修记录列表
	@PostMapping("/queryRequestAndRepair")
	public TableDataInfo queryRequestAndRepair(@RequestBody RequestAndRepairRecord requestAndRepairRecord) {
		startPage();
		List<RequestAndRepairRecord> list = reportInfoService.queryRequestAndRepair(requestAndRepairRecord);
		return getDataTable(list);
	}


	// 查询报修和维修记录详情
	@PostMapping("/queryRecordDetail")
	public AjaxResult queryRecordDetail(@RequestBody RequestAndRepairRecord requestAndRepairRecord) {
		AjaxResult ajax = AjaxResult.success();
		// 查询报修多媒体信息
		ajax.put("requestFiles", requestFileInfoService.SelectFileByRequestId(requestAndRepairRecord.getRequestId()));
		if (requestAndRepairRecord.getRepairId() != null) {
			// 查询维修多媒体信息
			ajax.put("repairFiles",requestFileInfoService.SelectRepairFileByRequestId(requestAndRepairRecord.getRepairId()));
			// 查询维修协助人
			ajax.put("heleUsers",repairRecordInfoService.selectHelpUserByRepairId(requestAndRepairRecord.getRepairId()));
			// 查询维修时间及工时
			RepairTimeRecord repairTimeRecord = new RepairTimeRecord();
			repairTimeRecord.setRepairId(requestAndRepairRecord.getRepairId());
			ajax.put("repairTime", reportInfoService.queryRepairTimeRecord(repairTimeRecord));
		}
		return ajax;
	}
}
